﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using University.Workshop.Model;
using System.Collections.ObjectModel;
using University.Workshop.DataAccess;
using System.IO;

namespace University.Workshop.WebApp
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            loadProductInfoE("Electronics");
            loadProductInfoF("Fashion");
            loadProductInfoDG("Deals & Gifts");
            loadProductInfoM("Motors");
            loadProductInfoEN("Entertainment");
            loadProductInfoH("Home, Outdoors Decor");

        }
        public void loadProductInfoE(string category)
        {
            Collection<Product> products = ProductDataAccess.GetProductsAuctionByCategory(category);
            if (products != null && products.Count != 0)
            {
                productsE.Visible = true;
                lvHeader.Text = "ELECTRONICS";
                lvHeader.Font.Bold = true;
                lvHeader.Font.Size = FontUnit.XLarge;
                lvHeader.ForeColor = System.Drawing.Color.FromName("#400040");
                TableRow tr = new TableRow();
                int max = (products.Count > 4) ? 4 : products.Count;
                for (int i = 0; i < max; i++)
                {
                    TableCell td = new TableCell();
                    LQSHome p = (LQSHome)Page.LoadControl("~/LQSHome.ascx");
                    Product product = ProductDataAccess.GetProductById(products[i].ProductId);
                    p.img.ImageUrl = "~/Images/noimage2.jpg";
                    if (Directory.Exists(Server.MapPath("~/Thumbnails/") + product.ProductId + "/"))
                    {
                        DirectoryInfo dir = new DirectoryInfo(Server.MapPath("~/Thumbnails/") + product.ProductId + "/");
                        FileInfo[] currentImages = dir.GetFiles();
                        string thumbUrl = "Thumbnails/" + product.ProductId + "/";
                        if (currentImages.Length > 0)
                        {
                            p.img.ImageUrl = thumbUrl + currentImages[0].Name;
                        }
                    }
                    p.hLink.Text = product.Name;
                    p.link += product.ProductId + "&ProductName=" + product.Name + "&ProductPrice=" + product.Price + "&ProductCategory=" + product.Category;
                    p.lPrice.Text = "Precio: " + product.Price;
                    if (product.Currency == "Dollars")
                        p.lPrice.Text += "$us.";
                    else
                        p.lPrice.Text += " Bs.";
                    td.Controls.Add(p);
                    td.VerticalAlign = VerticalAlign.Top;
                    tr.Cells.Add(td);
                }
                tblDynamic.Rows.Add(tr);
            }

        }

        public void loadProductInfoDG(string category)
        {
            Collection<Product> products = ProductDataAccess.GetProductsAuctionByCategory(category);
            if (products != null && products.Count != 0)
            {
                productsDG.Visible = true;
                lvHeader3.Text = "DEALS & GIFTS";
                lvHeader3.Font.Bold = true;
                lvHeader3.Font.Size = FontUnit.XLarge;
                lvHeader3.ForeColor = System.Drawing.Color.FromName("#400040");
                TableRow tr = new TableRow();
                int max = (products.Count > 4) ? 4 : products.Count;
                for (int i = 0; i < max; i++)
                {
                    TableCell td = new TableCell();
                    LQSHome p = (LQSHome)Page.LoadControl("~/LQSHome.ascx");
                    Product product = ProductDataAccess.GetProductById(products[i].ProductId);
                    p.img.ImageUrl = "~/Images/giftcard.jpg";
                    if (Directory.Exists(Server.MapPath("~/Thumbnails/") + product.ProductId + "/"))
                    {
                        DirectoryInfo dir = new DirectoryInfo(Server.MapPath("~/Thumbnails/") + product.ProductId + "/");
                        FileInfo[] currentImages = dir.GetFiles();
                        string thumbUrl = "Thumbnails/" + product.ProductId + "/";
                        if (currentImages.Length > 0)
                        {
                            p.img.ImageUrl = thumbUrl + currentImages[0].Name;
                        }
                    }
                    p.hLink.Text = product.Name;
                    p.link += product.ProductId + "&ProductName=" + product.Name + "&ProductPrice=" + product.Price + "&ProductCategory=" + product.Category;
                    p.lPrice.Text = "Precio: " + product.Price;
                    if (product.Currency == "Dollars")
                        p.lPrice.Text += "$us.";
                    else
                        p.lPrice.Text += " Bs.";
                    td.Controls.Add(p);
                    td.VerticalAlign = VerticalAlign.Top;
                    tr.Cells.Add(td);
                }
                tblDynamic3.Rows.Add(tr);
            }

        }
        public void loadProductInfoF(string category)
        {
            Collection<Product> products = ProductDataAccess.GetProductsAuctionByCategory(category);
            if (products != null && products.Count != 0)
            {
                productsF.Visible = true;
                lvHeader2.Text = "FASHION";
                lvHeader2.Font.Bold = true;
                lvHeader2.Font.Size = FontUnit.XLarge;
                lvHeader2.ForeColor = System.Drawing.Color.FromName("#400040");
                TableRow tr = new TableRow();
                int max = (products.Count > 4) ? 4 : products.Count;
                for (int i = 0; i < max; i++)
                {
                    TableCell td = new TableCell();
                    LQSHome p = (LQSHome)Page.LoadControl("~/LQSHome.ascx");
                    Product product = ProductDataAccess.GetProductById(products[i].ProductId);
                    p.img.ImageUrl = "~/Images/fashion.png";
                    if (Directory.Exists(Server.MapPath("~/Thumbnails/") + product.ProductId + "/"))
                    {
                        DirectoryInfo dir = new DirectoryInfo(Server.MapPath("~/Thumbnails/") + product.ProductId + "/");
                        FileInfo[] currentImages = dir.GetFiles();
                        string thumbUrl = "Thumbnails/" + product.ProductId + "/";
                        if (currentImages.Length > 0)
                        {
                            p.img.ImageUrl = thumbUrl + currentImages[0].Name;
                        }
                    }
                    p.hLink.Text = product.Name;
                    p.link += product.ProductId + "&ProductName=" + product.Name + "&ProductPrice=" + product.Price + "&ProductCategory=" + product.Category;
                    p.lPrice.Text = "Precio: " + product.Price;
                    if (product.Currency == "Dollars")
                        p.lPrice.Text += "$us.";
                    else
                        p.lPrice.Text += " Bs.";
                    td.Controls.Add(p);
                    td.VerticalAlign = VerticalAlign.Top;
                    tr.Cells.Add(td);
                }
                tblDynamic2.Rows.Add(tr);
            }

        }
        public void loadProductInfoEN(string category)
        {
            Collection<Product> products = ProductDataAccess.GetProductsAuctionByCategory(category);
            if (products != null && products.Count != 0)
            {
                productsEN.Visible = true;
                lvHeader5.Text = "ENTERTAINMENT";
                lvHeader5.Font.Bold = true;
                lvHeader5.Font.Size = FontUnit.XLarge;
                lvHeader5.ForeColor = System.Drawing.Color.FromName("#400040");
                TableRow tr = new TableRow();
                int max = (products.Count > 4) ? 4 : products.Count;
                for (int i = 0; i < max; i++)
                {
                    TableCell td = new TableCell();
                    LQSHome p = (LQSHome)Page.LoadControl("~/LQSHome.ascx");
                    Product product = ProductDataAccess.GetProductById(products[i].ProductId);
                    p.img.ImageUrl = "~/Images/entertainment.jpg";
                    if (Directory.Exists(Server.MapPath("~/Thumbnails/") + product.ProductId + "/"))
                    {
                        DirectoryInfo dir = new DirectoryInfo(Server.MapPath("~/Thumbnails/") + product.ProductId + "/");
                        FileInfo[] currentImages = dir.GetFiles();
                        string thumbUrl = "Thumbnails/" + product.ProductId + "/";
                        if (currentImages.Length > 0)
                        {
                            p.img.ImageUrl = thumbUrl + currentImages[0].Name;
                        }
                    }
                    p.hLink.Text = product.Name;
                    p.link += product.ProductId + "&ProductName=" + product.Name + "&ProductPrice=" + product.Price + "&ProductCategory=" + product.Category;
                    p.lPrice.Text = "Precio: " + product.Price;
                    if (product.Currency == "Dollars")
                        p.lPrice.Text += "$us.";
                    else
                        p.lPrice.Text += " Bs.";
                    td.Controls.Add(p);
                    td.VerticalAlign = VerticalAlign.Top;
                    tr.Cells.Add(td);
                }
                tblDynamic5.Rows.Add(tr);
            }

        }
        public void loadProductInfoH(string category)
        {
            Collection<Product> products = ProductDataAccess.GetProductsAuctionByCategory(category);
            if (products != null && products.Count != 0)
            {
                productsH.Visible = true;
                lvHeader6.Text = "HOME, OUTDOORS DECOR";
                lvHeader6.Font.Bold = true;
                lvHeader6.Font.Size = FontUnit.XLarge;
                lvHeader6.ForeColor = System.Drawing.Color.FromName("#400040");
                TableRow tr = new TableRow();
                int max = (products.Count > 4) ? 4 : products.Count;
                for (int i = 0; i < max; i++)
                {
                    TableCell td = new TableCell();
                    LQSHome p = (LQSHome)Page.LoadControl("~/LQSHome.ascx");
                    Product product = ProductDataAccess.GetProductById(products[i].ProductId);
                    p.img.ImageUrl = "~/Images/homedecor.gif";
                    if (Directory.Exists(Server.MapPath("~/Thumbnails/") + product.ProductId + "/"))
                    {
                        DirectoryInfo dir = new DirectoryInfo(Server.MapPath("~/Thumbnails/") + product.ProductId + "/");
                        FileInfo[] currentImages = dir.GetFiles();
                        string thumbUrl = "Thumbnails/" + product.ProductId + "/";
                        if (currentImages.Length > 0)
                        {
                            p.img.ImageUrl = thumbUrl + currentImages[0].Name;
                        }
                    }
                    p.hLink.Text = product.Name;
                    p.link += product.ProductId + "&ProductName=" + product.Name + "&ProductPrice=" + product.Price + "&ProductCategory=" + product.Category;
                    p.lPrice.Text = "Precio: " + product.Price;
                    if (product.Currency == "Dollars")
                        p.lPrice.Text += "$us.";
                    else
                        p.lPrice.Text += " Bs.";
                    td.Controls.Add(p);
                    td.VerticalAlign = VerticalAlign.Top;
                    tr.Cells.Add(td);
                }
                tblDynamic6.Rows.Add(tr);
            }

        }
        public void loadProductInfoM(string category)
        {
            Collection<Product> products = ProductDataAccess.GetProductsAuctionByCategory(category);
            if (products != null && products.Count != 0)
            {
                productsM.Visible = true;
                lvHeader4.Text = "MOTORS";
                lvHeader4.Font.Bold = true;
                lvHeader4.Font.Size = FontUnit.XLarge;
                lvHeader4.ForeColor = System.Drawing.Color.FromName("#400040");
                TableRow tr = new TableRow();
                int max = (products.Count > 4) ? 4 : products.Count;
                for (int i = 0; i < max; i++)
                {
                    TableCell td = new TableCell();
                    LQSHome p = (LQSHome)Page.LoadControl("~/LQSHome.ascx");
                    Product product = ProductDataAccess.GetProductById(products[i].ProductId);
                    p.img.ImageUrl = "~/Images/motor.jpg";
                    if (Directory.Exists(Server.MapPath("~/Thumbnails/") + product.ProductId + "/"))
                    {
                        DirectoryInfo dir = new DirectoryInfo(Server.MapPath("~/Thumbnails/") + product.ProductId + "/");
                        FileInfo[] currentImages = dir.GetFiles();
                        string thumbUrl = "Thumbnails/" + product.ProductId + "/";
                        if (currentImages.Length > 0)
                        {
                            p.img.ImageUrl = thumbUrl + currentImages[0].Name;
                        }
                    }
                    p.hLink.Text = product.Name;
                    p.link += product.ProductId + "&ProductName=" + product.Name + "&ProductPrice=" + product.Price + "&ProductCategory=" + product.Category;
                    p.lPrice.Text = "Precio: " + product.Price;
                    if (product.Currency == "Dollars")
                        p.lPrice.Text += "$us.";
                    else
                        p.lPrice.Text += " Bs.";
                    td.Controls.Add(p);
                    td.VerticalAlign = VerticalAlign.Top;
                    tr.Cells.Add(td);
                }
                tblDynamic4.Rows.Add(tr);
            }

        }
    }
}
